package com.ytjj.qmyx.mall.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.model.UsersResponse;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.mall.mapper.AdvertCallbackMapper;
import com.ytjj.qmyx.mall.model.AdvertCallback;
import com.ytjj.qmyx.mall.model.AdvertCallbackExample;
import com.ytjj.qmyx.mall.model.EffectParamRequest;
import com.ytjj.qmyx.mall.service.AdvertCallBackService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
@RefreshScope
public class AdvertCallBackServiceImpl implements AdvertCallBackService {

    @Resource
    private AdvertCallbackMapper advertCallbackMapper;

    @Value("${effect.url:https://activity.tuia.cn/log/effect/v2}")
    private String effectUrl;

    @Value("${effect.advertKey:F5D11DC74674875BBD6CB86A503AE6ED}")
    private String advertKey;

    @Resource
    private RedisService redisService;

    @Override
    public void saveAdvertCallBack(AdvertCallback advertCallback) {
        AdvertCallbackExample advertCallbackExample=new AdvertCallbackExample();
        AdvertCallbackExample.Criteria criteria=advertCallbackExample.createCriteria();
        criteria.andAdvertPlatformEqualTo(advertCallback.getAdvertPlatform());
        criteria.andTypeEqualTo(advertCallback.getType());
        if("android".equals(advertCallback.getPlatform())) {
            criteria.andImeiEqualTo(advertCallback.getImei());
        }else if("ios".equals(advertCallback.getPlatform())) {
            criteria.andIdfaEqualTo(advertCallback.getIdfa());
        }else{
            return;
        }

        List<AdvertCallback> advertCallbackList=advertCallbackMapper.selectByExample(advertCallbackExample);
        if(null!=advertCallbackList&&advertCallbackList.size()>0){
            return;
        }
        advertCallback.setCreateTime(new Date());
        advertCallback.setUpdateTime(new Date());
        advertCallbackMapper.insert(advertCallback);
    }

    @Override
    public AdvertCallback getAdvertCallBack(AdvertCallback advertCallback) {
        AdvertCallbackExample advertCallbackExample=new AdvertCallbackExample();
        AdvertCallbackExample.Criteria criteria=advertCallbackExample.createCriteria();
        criteria.andAdvertPlatformEqualTo(advertCallback.getAdvertPlatform());
        criteria.andTypeEqualTo(advertCallback.getType());
        if("android".equals(advertCallback.getPlatform())) {
            criteria.andImeiEqualTo(advertCallback.getImei());
        }else if("ios".equals(advertCallback.getPlatform())) {
            criteria.andIdfaEqualTo(advertCallback.getIdfa());
        }else{
            return null;
        }

        List<AdvertCallback> advertCallbackList=advertCallbackMapper.selectByExample(advertCallbackExample);
        if(null!=advertCallbackList&&advertCallbackList.size()>0){
            return advertCallbackList.get(0);
        }
        return null;
    }

    @Override
    public String sendEffectData(EffectParamRequest request) {
        return "";
//        Map map = new HashMap<>();
//        map.put("a_oId", request.getA_oId());
//        map.put("advertKey", advertKey);
//        map.put("subType", request.getSubType());
//        if (request.getType() != null && request.getSubType() != null && request.getSubType() == 0) {
//            map.put("type", 8);
//        }
//
//        if (request.getSubType() != null && (request.getSubType() == 3 || request.getSubType() == 6)) {
//            map.put("payment_amount", request.getPaymentAmount());
//        }
//        if (request.getSubType() != null && request.getSubType() == 10) {
//            String usesInfo = redisService.get(request.getH5Token());
//            String a_oId = "";
//            if (StringUtils.isNotBlank(usesInfo)) {
//                UsersResponse usersResponse = JSONObject.parseObject(usesInfo, UsersResponse.class);
//                if (StringUtils.isNotBlank(usersResponse.getAoId())) {
//                    a_oId =  usersResponse.getAoId();
//                }
//            }
//            map.put("a_oId", a_oId);
//            map.put("payment_amount", "-"+request.getPaymentAmount());
//        }
////        log.info("请求推呀参数:{}",JSONObject.toJSON(map));
//        String result = HttpClientUtil.doGetNew(effectUrl, map);
////        log.info("请求推呀返回:{}",result);
//        return result;
    }

}
